<template>
	<view class="body">
		<!-- 轮播图 -->
		<u-swiper height="124" radius="0" bottom :list="swiperImages" indicator indicatorMode="line" circular
			indicatorActiveColor="#2393FF" indicatorInactiveColor="#FFFFFF"></u-swiper>
		<!-- <image class="banner" src="https://store-profit-system.oss-cn-shanghai.aliyuncs.com/uniappImg/classBanner.png" mode=""></image> -->
		<!-- 商品展示区域 -->
		<view v-for="(products, categoryId) in categoryProducts" :key="categoryId" >
			<!-- 显示分类名称 -->
			<view class="titleBig">
				<view class="lineText"></view>{{ getCategoryName(categoryId) }}
			</view>
			<!-- 显示商品列表 -->
			<view class="productGrid">
				<view class="commodityBox" v-for="(item, index) in products" :key="index" @click="godDetail(item)">
					<image class="image" :src="item.picture" mode=""></image>
					<view class="productInfo">
						<view class="">
							<p class="title">{{ item.goodsName }}</p>
							<view class="tip">{{tipMap[item.goodsName]}}</view>
						</view>
						<view class="flex-between">
							<view class="">
								<p class="oldMoney"> 原价:¥{{ item.msqGoodsAttrValueList[0].price || '无价格信息' }}</p>
								<p class="money">￥{{ item.msqGoodsAttrValueList[0].price || '无价格信息' }}</p>
							</view>
							<button class="btn">立即购买 <text>{{arrow}}</text></button>
						</view>
						<text class="tag" v-if="item.tagId">{{ item.tagName }}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		postGoodsType,
		postGoodsPage
	} from '@/api/cate';
	import {
		getBanner
	} from '@/api/user.js';
	export default {
		data() {
			return {
				menus: [],
				categoryProducts: {},
				swiperImages: [
					'https://store-profit-system.oss-cn-shanghai.aliyuncs.com/uniappImg/classBanner.png',
					'https://store-profit-system.oss-cn-shanghai.aliyuncs.com/uniappImg/classBanner.png'
				],
				where: {
					page: 1,
					limit: 20,
					isDel: '1',
					goodsName: null,
					keyword: null,
				},
				tipMap:{
					'爆客标准版':'引流获客 裂变推广 爆品打造',
					'运营专业版':'会员运营 营销活动 创客孵化',
					'商业旗舰版':'渠道营销  股东融资 数据决策',
					'星推官—流量引擎':'流量引擎 资源变现 圈层融合',
					'代言人—全域裂变':'市场拓展 培训赋能 价值传递',
					'服务商—智库赋能':'智库赋能 渠道服务 互利共赢',
				},
				arrow:'>>>'
			};
		},
		onLoad() {
			this.fetchAllCategoriesAndProducts();
			this.getAllImage()
		},
		methods: {
			// 获取所有分类及其商品
			fetchAllCategoriesAndProducts() {
				postGoodsType({
					"status": null,
					"isOpen":0,
				}).then(res => {
					const records = res.data.records || [];
					this.menus = records.map(record => ({
						acquiesce: `../../static/category/category_${record.id * 2 - 1}.png`,
						select: `../../static/category/category_${record.id * 2}.png`,
						id: record.id,
						goodsType: record.goodsType
					}));
					records.forEach(record => this.get_product_list(record.id));
				});
			},
			// 查找产品
			get_product_list(categoryId) {
				
				let where = Object.assign({}, this.where, {
					goodsTypeId: categoryId,
					page: 1
				});
				postGoodsPage(where).then(res => {
					let list = res.data.records || [];
					for (var i = 0; i < list.length; i++) {
						list[i].blurb = list[i].blurb ? list[i].blurb.replace(/<[^>]+>/g, '') : '';
					}
					this.$set(this.categoryProducts, categoryId, list);
					console.log(this.categoryProducts);
				}).catch(err => {});
			},
			// 搜索
			searchSubmit(e) {
				this.where.goodsName = e.value;
				this.where.page = 1;
				this.categoryProducts = {};
				if (this.where.goodsName) {
					// 如果有搜索关键词，则只查询第一个分类的商品
					this.get_product_list(Object.keys(this.categoryProducts)[0] || this.menus[0]?.id);
				} else {
					// 如果没有搜索关键词，则重新加载所有分类的商品
					this.fetchAllCategoriesAndProducts();
				}
			},
			// 去详情页
			godDetail(item) {
				console.log(item,"item")
				uni.navigateTo({
					url: `/pages/goods_details/index?id=${item.id}`
				});
			},
			// 获取分类名称
			getCategoryName(categoryId) {
				const menu = this.menus.find(menu => menu.id == categoryId);
				return menu ? menu.goodsType : '未知分类';
			},
			// 获取轮播图
			getAllImage(){
				getBanner({typeName:'加入我们轮播图'}).then(res=>{
					this.swiperImages =res.data.records.map(item=>item.cover)
				})
			},
		}
	};
</script>

<style scoped lang="scss">
	.body{
		padding: 24rpx 32rpx;
	}
	.searchBox {
		background-color: #fff;
	}
	.banner{
		width: 95%;
		height: 240rpx;
		margin: 10rpx 20rpx;
	}
	.categoryName {
		font-size: 30rpx;
		margin: 20rpx 0;
		font-weight: bold;
	}

	.productGrid {
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
		gap: 20rpx;
	}

	.commodityBox {
		// width: calc(50% - 12rpx);
		width: 100%;
		background: #FFFFFF;
		border-radius: 24rpx;
		box-sizing: border-box;
		padding: 40rpx 22rpx 24rpx;
		position: relative;
		display: flex;
		flex-wrap: nowrap;
		
		.image {
			width: 198rpx;
			height: 174rpx;
			border-radius: 14rpx;
		}

		.productInfo {
			width: calc(100% - 198rpx);
			padding:0rpx 0 0 20rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			.title {
				font-size: 32rpx;
				font-weight: 700;
				color: #28314C;
				// word-break: break-all;
				// overflow: hidden;
				// text-overflow: ellipsis;
				// display: -webkit-box;
				// -webkit-line-clamp: 2;
				// -webkit-box-orient: vertical;
				// text-align: left;
				// font-weight: bold;
			}
			.tag{
				background: linear-gradient( 180deg, #FF700A 0%, #FE980B 100%);
				border-radius: 0px 14rpx 0px 18rpx;
				font-size: 24rpx;
				color: #FFFFFF;
				padding: 4rpx 24rpx;
				position: absolute;
				right: 0;
				top: 0;
			}
			.oldMoney{
				font-size: 24rpx;
				color: #D2D2D2;
				text-decoration: line-through;
			}
			.money {
				font-size: 30rpx;
				color: #F9423A;
				font-weight: 700;
				margin-top: 6rpx;
				font-family:'DIN';
				// text-align: right;
			}
			.btn{
				width: 178rpx;
				height: 62rpx;
				background: #0077FF;
				border-radius: 42rpx;
				font-size: 24rpx;
				color: #FFFFFF;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
	.titleBig {
		padding: 22rpx 24rpx 22rpx 0;
		font-weight: 700;
		font-size: 32rpx;
		color: #28314C;
		display: flex;
		align-items: center;
	}
	.lineText {
		width: 8rpx;
		height: 34rpx;
		background: #2393FF;
		border-radius: 4px 4px 4px 4px;
		margin-right: 12rpx;
	}
	.flex-between{
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	.tip{
		font-size: 24rpx;
		color: #D2D2D2;
		margin-top: 10rpx;
	}
</style>